import { login } from '@/api/user'
import { setToken, getToken, removeToken } from '@/utils/auth'
const state = {
  token: getToken()
}
const mutations = {
  // 设置token
  setToken(state, payload) {
    state.token = payload
    setToken(payload)
  },
  // 退出登录
  logout(state) {
    state.token = null
    removeToken()
  }
}
const actions = {
  // 登录
  async login(context, payload) {
    try {
      const res = await login(payload)
      // console.log(res)
      context.commit('setToken', res.token)
    } catch (err) {
      console.log(err)
    }
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}
